package com.study.portal.controller.interceptor;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;
import com.study.portal.util.StrutsUtil;
import com.study.portal.util.bean.LoginUesrBean;
import com.study.portal.util.bean.MessageInfoBean;

@SuppressWarnings("serial")
public class LoginUserBeanInterceptor implements Interceptor {
	
	private Logger logger = Logger.getLogger(LoginUserBeanInterceptor.class);
	public void destroy() {
		logger.debug("LoginUserBeanInterceptor 拦截器已销毁...");
	}

	public void init() {
		logger.debug("LoginUserBeanInterceptor 拦截器初始化成功...");
	}

	public String intercept(ActionInvocation invocation) throws Exception {
		//判断用户是否登录
		LoginUesrBean loginUesrBean = StrutsUtil.getLoginUserBean();
		if(null==loginUesrBean){
			MessageInfoBean messageInfoBean = new MessageInfoBean();
			Map<String,String> params = new HashMap<String, String>();
			params.put("url", StrutsUtil.getRequestUrl());
			messageInfoBean.setRedirect(StrutsUtil.getActionUrl("login",params));
			messageInfoBean.addMsg("亲！你还没有登录.");
			HttpSession session = ServletActionContext.getRequest().getSession();
			session.setAttribute("messageInfoBean", messageInfoBean);
			return "msg";
		}
		return invocation.invoke();
	}

}
